package main

import (
	"github.com/gogap/logs"
	"log"
	"yundian/internel/app/dao"
	"yundian/internel/app/models/orders"
	"yundian/internel/app/models/users"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

func main() {
	//dingo.InitViper("./configs/adm.yaml")
	//dingo.InitViper("./configs/zmtest-adm.yaml")
	dingo.InitViper("./configs/zhongman.yaml")
	dbs.NewMysqlComponent().Step()

	var tradeNoList221 []string
	var tradeNoList217 []string

	Sql221 := `SELECT o.trade_no FROM orders o WHERE user_id<>221
	AND place_id NOT IN
	    (20111,20113,20122,20143,20144,20145,20146,20246,20247,20250,20312,20336,20346,20347,20348,20391,20440,20516,20519,20535,20573,20576,20577,20578,20579,20582,20584,20586,20587,20593,20594,20597,20598,20601,20604,20607,20608,20609,20614,20615,20618,20619,20622,20631,20632,20636,20640,20641,20642,20645,20646,20648,20649,20651,20652,20654,20655,20657,20659,20660,20661,20663,20665,20668,20670,20671,20672,20677,20678,20684,20690,20691,20692,20693,20694,20699,20708,20711,20722,20723,20745,20748,20750,20751,20752,20753,20757,20758,20772,20773,20776,20778,20779,20780,20797,20806,20809,20814,20818,20827,20828,20829,20864,20890,20901,20911,20917,20939,20971,20974,20975,20976,20982,20986,21018,21021,21027,21029,21032,21050,21052,21062,21063,21076,21096,21099,21102,21113,21115,21118,21122,21124,21131,21158,21178,21203,30491,30496,30501,30509,30512,30515,30527,30534,30537,30547,30559,30750,30767,30769,30778,30805,30923,30968,30969,30972,30973,30981,30983,30986,30987,31027,31062,31086,31166,31177,31424,31503,31604,31653,31687,31689,31694,31715,31752,31754,31770,31820,31902,31936,31943,31946,31947,31957,31958,31959,31960,31961,32214,442077,442089,442091,442137,442142,442256,442264,442265,442267,442336,442390,442391,442393,442394,442397,442403,442405,442409,442413,442414,442417,442418,442424,442458,442459,442463,442465,442468,442470,442471,442472,442473,442474,442475,442531,442534,442535,442536,442574,442589,442590,442591,442601,442603,442608,442662,442750,442755,442761,442766,442767,442886,442896,443035,443038,443044,443095,443137,444045,444117,444144,444213
	    )
	AND trade_no IN (
	    SELECT dl.trade_no FROM (
	SELECT dl.trade_no,dl.place_name FROM dividends_log dl WHERE user_id IN (221)
	) dl
	LEFT JOIN test_221 t1 ON dl.place_name=t1.place_name WHERE t1.place_name IS NULL
	) ORDER BY o.trade_no`
	if db := global.Orm.Debug().Raw(Sql221).Find(&tradeNoList221); db.Error != nil {
		logs.Error(db.Error)
		return
	}
	Sql217 := `SELECT o.trade_no FROM orders o WHERE user_id<>217
	AND place_id NOT IN
	    (
	     20700,20702,20932,20942,20947,20985,20992,20993,20994,20995,20996,20997,21107,21140,21147,30546,30783,30853,30921,31017,31021,31023,31024,31026,31028,31029,31032,31134,31138,31140,31141,31212,31213,31256,31328,31333,31334,31335,31359,31360,31361,31362,31363,31369,31370,31371,31373,31375,31399,31400,31401,31426,31548,31566,31576,31577,31578,31612,31614,31615,31620,31621,31622,31628,31644,31649,31652,31659,31673,31721,31787,31796,31876,31887,31888,31894,31895,31896,31897,31898,31977,31987,32030,32038,441682,441693,441703,441704,441736,441740,441742,441990,442029,442072,442073,442116,442117,442119,442125,442174,442175,442184,442185,442320,442335,442375,442381,442382,442489,442592,442613,442619,442769,442789,442792,442794,442795,443718,443850,444157,444184
	)
	AND trade_no IN (
	    SELECT dl.trade_no FROM (
	SELECT dl.trade_no,dl.place_name FROM dividends_log dl WHERE user_id IN (217)
	) dl
	LEFT JOIN test_217 t1 ON dl.place_name=t1.place_name WHERE t1.place_name IS NULL
	) ORDER BY o.trade_no`
	if db := global.Orm.Debug().Raw(Sql217).Find(&tradeNoList217); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	if db := global.Orm.Debug().Exec("DELETE FROM orders_user_relevancy WHERE trade_no IN (?)", append(tradeNoList221, tradeNoList217...)); db.Error != nil {
		logs.Error(db.Error)
		return
	}
	if db := global.Orm.Debug().Exec("DELETE FROM dividends_log WHERE trade_no IN (?)", append(tradeNoList221, tradeNoList217...)); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	var orderListUse []orderListUseType
	orderSql := `SELECT o.*,p.branch_user_id,DATE_FORMAT(FROM_UNIXTIME(o.end_at), '%Y-%m-%d')end_date,DATE_FORMAT(
	FROM_UNIXTIME(o.end_at), '%Y-%m')end_month FROM orders o LEFT JOIN places p ON o.parent_id=p.id WHERE o.trade_no in (?) 
	ORDER BY o.id`
	if db := global.Orm.Raw(orderSql, append(tradeNoList221, tradeNoList217...)).Find(&orderListUse); db.Error != nil {
		logs.Error(db.Error)
		return
	}
	OrderRelevanceSyncDataExec(orderListUse, 300)
}

//type orderListUseType struct {
//	orders.OrderModel
//	BranchUserId int    `gorm:"column:branch_user_id"`
//	EndDate      string `gorm:"column:end_date"`
//	EndMonth     string `gorm:"column:end_month"`
//}
//
//// 订单关联执行，非分红表
//func OrderRelevanceSyncDataExec(orderListUse []orderListUseType, max int) {
//	if len(orderListUse) == 0 {
//		return
//	}
//
//	userMap := map[int][]int{}
//	insertList := []map[string]interface{}{}
//	for index, item := range orderListUse {
//		if userMap[item.UserId] == nil {
//			userMap[item.UserId] = toRepetition(dao.NewUserDao().QueryParentList(item.UserId, item.BranchUserId).Unwrap().([]*users.UserModel))
//		}
//		for _, userId := range userMap[item.UserId] {
//			insertList = append(insertList, map[string]interface{}{
//				"user_id":   userId,
//				"id":        item.Id,
//				"trade_no":  item.TradeNo,
//				"end_date":  item.EndDate,
//				"end_month": item.EndMonth,
//			})
//		}
//
//		if len(insertList) >= max || index == len(orderListUse)-1 {
//			if db := global.Orm.Table("orders_user_relevancy").Create(insertList); db.Error != nil {
//				log.Println("orders_user_relevancy ERR", db.Error, item)
//				return
//			}
//			insertList = insertList[:0]
//			log.Println("成功", item)
//		}
//	}
//}
//
//func toRepetition(list []*users.UserModel) []int {
//	var newList []int
//	for _, item := range list {
//		off := true
//		for _, id1 := range newList {
//			if item.ID == id1 {
//				off = false
//				break
//			}
//		}
//		if off {
//			newList = append(newList, item.ID)
//		}
//	}
//	return newList
//}
